<?php

class UEditorController extends Controller{
	
	private $stateMap = array( //上传状态映射表，国际化用户需考虑此处数据的国际化
			"SUCCESS", //上传成功标记，在UEditor中内不可改变，否则flash判断会出错
			"文件大小超出 upload_max_filesize 限制",
			"文件大小超出 MAX_FILE_SIZE 限制",
			"文件未被完整上传",
			"没有文件被上传",
			"上传文件为空",
			"ERROR_TMP_FILE" => "临时文件错误",
			"ERROR_TMP_FILE_NOT_FOUND" => "找不到临时文件",
			"ERROR_SIZE_EXCEED" => "文件大小超出网站限制",
			"ERROR_TYPE_NOT_ALLOWED" => "文件类型不允许",
			"ERROR_CREATE_DIR" => "目录创建失败",
			"ERROR_DIR_NOT_WRITEABLE" => "目录没有写权限",
			"ERROR_FILE_MOVE" => "文件保存时出错",
			"ERROR_FILE_NOT_FOUND" => "找不到上传文件",
			"ERROR_WRITE_CONTENT" => "写入文件内容错误",
			"ERROR_UNKNOWN" => "未知错误",
			"ERROR_DEAD_LINK" => "链接不可用",
			"ERROR_HTTP_LINK" => "链接不是http链接",
			"ERROR_HTTP_CONTENTTYPE" => "链接contentType不正确",
			"INVALID_URL" => "非法 URL",
			"INVALID_IP" => "非法 IP"
	);
	
	private $base64; //文件上传对象
	private $config; //配置信息
	private $oriName; //原始文件名
	private $fileName; //新文件名
	private $fullName; //完整文件名,即从当前配置目录开始的URL
	private $filePath; //完整文件名,即从当前配置目录开始的URL
	private $fileSize; //文件大小
	private $fileType; //文件类型
	private $stateInfo; //上传状态信息,
	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		//Log::error($agent);
		if (is_null($this->config)) {
			$this->config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(public_path("assets/js/ueditor/config.json"))), true);
		}
	}
	
	public function service(){
		$action = Input::get("action");
		
		switch ($action) {
			case 'config':
				$result =  json_encode($this->config);
				break;
				/* 上传图片 */
			case 'uploadimage':
				/* 上传涂鸦 */
			case 'uploadscrawl':
				/* 上传视频 */
			case 'uploadvideo':
				/* 上传文件 */
			case 'uploadfile':
				$this->upfile();
				$result = json_encode($this->getFileInfo());
				break;
				/* 列出图片 */
			case 'listimage':
				break;
				/* 列出文件 */
			case 'listfile':
				break;
		
				/* 抓取远程文件 */
			case 'catchimage':
				break;
			default:
				$result = json_encode(array(
					'state'=> '请求地址出错'
						));
				break;
		}
		
		return $result;
	}
	
	
	private function upfile(){
		$fieldName = $this->config["imageFieldName"];
		//$maxSize = $this->config["imageMaxSize"]
		//Log::error("fieldName ".$this->config["videoFieldName"]);
		
		
// 		"maxSize" => $CONFIG['imageMaxSize'],
// 		"allowFiles" => $CONFIG['imageAllowFiles']
		if (!Input::hasFile($fieldName)){
			$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
			return;
		}
		$file = Input::file($fieldName);
		
		if (!$file->isValid()){
			$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
			return;
		}
		
		$this->oriName = $file->getClientOriginalName();
		$this->fileSize = $file->getSize();
		$this->fileType = ".".$file->getClientOriginalExtension();
		
		
			
		//检查文件大小是否超出限制
		if (!$this->checkSize()) {
			$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
			return;
		}
		
		//检查是否不允许的文件格式
		if (!$this->checkType()) {
			$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
			return;
		}
		
		//上传文件，获取文件的key
		$this->fileName = $this->storeFile($file);
		$this->fullName = Config::get('mao.image_domain').$this->fileName;//url路径
		$this->stateInfo = $this->stateMap[0];
	}
	
	private function storeFile($file){
		$action = Input::get("action");
		switch ($action) {
			case 'uploadimage':/* 上传图片 */
				return Images::storeImage($file);
				break;
		
		
			case 'uploadvideo': /* 上传视频 */
				return Images::storeVedio($file,$this->fileType);
			default:
				$this->stateInfo = $this->getStateInfo("ERROR_UNKNOWN");
				return;
		}
	}
	/**
	 * 获取当前上传成功文件的各项信息
	 * @return array
	 */
	private function getFileInfo()
	{
		return array(
				"state" => $this->stateInfo,
				"url" => $this->fullName,
				"title" => $this->fileName,
				"original" => $this->oriName,
				"type" => $this->fileType,
				"size" => $this->fileSize
		);
	}
	
	/**
	 * 文件类型检测
	 * @return bool
	 */
	private function checkType(){
		$action = Input::get("action");
		switch ($action) {
			case 'uploadimage':/* 上传图片 */
				$types =  $this->config["imageAllowFiles"];
				break;
		
		
			case 'uploadvideo': /* 上传视频 */
				$types = $this->config["videoAllowFiles"];
				break;
			default:
				$types = array();
		}
		return in_array($this->fileType,$types);
	}
	
	/**
	 * 文件大小检测
	 * @return bool
	 */
	private function  checkSize() {
		$action = Input::get("action");
		switch ($action) {
			case 'uploadimage':/* 上传图片 */
				$size =  $this->config["imageMaxSize"];
				break;
				
				
			case 'uploadvideo': /* 上传视频 */
				$size = $this->config["videoMaxSize"];
				break;
			default:
				$size = 0;
		}
		return $this->fileSize <= $size;
	}
	
	/**
	 * 上传错误检查
	 * @param $errCode
	 * @return string
	 */
	private function getStateInfo($errCode) {
		return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode];
	}
}
?>